﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_Header : System.Web.UI.UserControl
{
    static DataTable tbGioHang = new DataTable();
    BUS_Menu busMenu = new BUS_Menu();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Literal1.Text = "<ul class='btn-login'><li><a href='Dangnhap.aspx'>Đăng nhập</a></li><li><a href='Dangky.aspx'>Đăng ký</a></li></ul>";
            Literal2.Text = "";
        }
        else
        {
            Literal2.Text = "<ul class='box-login-success'>";
            Literal2.Text += "\n<li>";
            Literal2.Text += "\n<p>Cá nhân</p><img src='http://sinhviendongthap.net/@4r/images/Windows7/topmenu/mtxuong.png' alt='' />";

            Literal2.Text += "\n<ul class='cn-link'>";
            Literal2.Text += "<li class='box-image'><a href='Trangcanhan.aspx'><img src='http://www.large-icons.com/stock-icons/large-user/customer-icon.gif'/></a></li>";
            Literal2.Text += "\n<li><a rel='nofollow' href='Trangcanhan.aspx'>Thông tin cá nhân</a></li>";
            Literal2.Text += "\n<li><a rel='nofollow' href='Trangcanhan.aspx?action=doithongtin'>Thay đổi thông tin</a></li>";
            Literal2.Text += "\n<li><a rel='nofollow' href='Trangcanhan.aspx?action=doimatkhau'>Đổi mật khẩu</a></li>";
            Literal2.Text += "\n<li><a rel='nofollow' href='Thoat.aspx'>Thoát</a></li>";
            Literal2.Text += "\n</ul>\n</li>";
            Literal2.Text += "<li><a href='Thoat.aspx'>Thoát</a></li>\n</ul>";
        }
        if (!IsPostBack)
        {
            ltrMain_menu.Text = displayMenu(1);
            ltrNav_menu.Text = displayMenu(2);
            if (Session["GioHang"] != null)
            {
                tbGioHang = (DataTable)Session["GioHang"];
                if (tbGioHang.Rows.Count > 0)
                {
                    lblSoLuongSP.Text = tbGioHang.Compute("Sum(SoLuong)", "").ToString();
                    lblTongTien.Text = string.Format("{0:0,0 VND}", float.Parse(tbGioHang.Compute("Sum(TongTien)", "").ToString()));
                }
                else
                {
                    lblSoLuongSP.Text = "0";
                    lblTongTien.Text = "0 VND";
                }

            }
            else
            {
                lblSoLuongSP.Text = "0";
                lblTongTien.Text = "0 VND";
            }

            time.Text = ngaythang();
            Timer1.Enabled = true;
            Timer1.Interval = 1000;
        }
    }
    public string ngaythang()
    {
        string thu = string.Format("{0:ddd}", DateTime.Now);
        string thu1 = "";
        switch (thu)
        {
            case "Mon":
                thu1 = "Thứ hai,";
                break;
            case "Tue":
                thu1 = "Thứ ba,";
                break;
            case "Wed":
                thu1 = "Thứ tư,";
                break;
            case "Thu":
                thu1 = "Thứ năm,";
                break;
            case "Fri":
                thu1 = "Thứ sáu,";
                break;
            case "Sat":
                thu1 = "Thứ bảy,";
                break;
            case "Sun":
                thu1 = "Chủ nhật,";
                break;
            default:
                break;
        }
        string t = thu1 + " " + string.Format("{0:dd/MM/yyyy hh:mm:ss tt}", DateTime.Now);
        return t;
    }

    public string LoadMenu1(string ID)
    {
        string menu = "";
        var dt = busMenu.Menu_GetById(ID);
        var dtChild = busMenu.Menu_GetByParentId(ID);
        if (dtChild.Rows.Count > 0)
        {
            menu += "\n<li><a href='" + dt.Rows[0]["Link"] + "'><img src='Images/" + dt.Rows[0]["Img"] + "' alt='' /><p>" + dt.Rows[0]["Name"] + "</p></a>";
            menu += "\n<ul>";
            foreach (DataRow r in dtChild.Rows)
            {
                //LoadMenu(r["Id"].ToString());
                menu += "\n<li><a href='" + r["Link"] + "'><img src='Images/" + r["Img"] + "' alt='' /><p>" + r["Name"] + "</p></a></li>";
            }
            menu += "\n</ul></li>";
        }
        else
        {
            menu += "\n<li><a href='" + dt.Rows[0]["Link"] + "'><img src='Images/" + dt.Rows[0]["Img"] + "' alt='' /><p>" + dt.Rows[0]["Name"] + "</p></a></li>";
        }

        return menu;
    }
    public string LoadMenu2(string ID)
    {
        string menu = "";
        var dt = busMenu.Menu_GetById(ID);
        var dtChild = busMenu.Menu_GetByParentId(ID);
        if (dtChild.Rows.Count > 0)
        {
            menu += "\n<li><a href='" + dt.Rows[0]["Link"] + "'><p>" + dt.Rows[0]["Name"] + "</p></a>";
            menu += "\n<ul>";
            foreach (DataRow r in dtChild.Rows)
            {
                //LoadMenu(r["Id"].ToString());
                menu += "\n<li><a href='" + r["Link"] + "'><p>" + r["Name"] + "</p></a></li>";
            }
            menu += "\n</ul></li>";
        }
        else
        {
            menu += "\n<li><a href='" + dt.Rows[0]["Link"] + "'><p>" + dt.Rows[0]["Name"] + "</p></a></li>";
        }

        return menu;
    }
    public string displayMenu(int location)
    {
        string menu = "";
        var dt = busMenu.getMenu(location);
        if (location == 1)
        {
            foreach (DataRow r in dt.Rows)
            {
                menu += LoadMenu1(r["Id"].ToString());
            }
        }
        else
        {
            foreach (DataRow r in dt.Rows)
            {
                menu += LoadMenu2(r["Id"].ToString());
            }
        }
        return menu;
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        //time.Text = DateTime.Now.DayOfWeek.ToString() + " " + DateTime.Now.ToString();
        time.Text = ngaythang();
    }
    
}